package com.furschina.service;

import java.util.Collection;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.wooshai.core.query.Expression;
import cn.wooshai.core.query.Query;
import cn.wooshai.core.query.QueryFactory;
import cn.wooshai.core.service.local.AbstractService;

import com.furschina.dao.ArticleDao;
import com.furschina.model.Article;
import com.furschina.vo.ArticlePage;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class ArticleService extends AbstractService<ArticleDao, Article> {

	/**
	 * 根据频道编号查询相关的资讯
	 * 
	 * @param pm
	 * @return
	 */
	public Collection<Article> findByChannel(ArticlePage pm
	/* 在action中实现PaginationAware时需要用到PageModel 用来临时存放翻页信息和参数 */) {
		Query q = QueryFactory.createQuery(Article.class).select(
				"id as id, title as title, summary as summary, "
						+ "cover as cover, publishDate as publishDate, creator as creator");
		if (pm.getChannelId() != null) {
			q = q.where(Expression.equal("channel.id", pm.getChannelId()));
		}
		return this.queryAll(q, pm, true/* 最后一个参数表示是否加入到缓存中 */);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public Article findForView(Long id) {
		return this.findByPrimaryKey(id);
	}
}
